/**
 * 
 */
package in.electromedicalabs.moviesorter;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * Provides APIs to perform basic HTTP operations
 * 
 * @author @nkit Prasad & Bhavin Mehta
 * @version $Revision: 1.0 $
 */
public class HttpUtils {
	/**
	 * Field httpclient.
	 */
	private static HttpClient httpclient;

	/**
	 * Initialized the client and makes it usable
	 */
	public static void initialize() {
		httpclient = new DefaultHttpClient();
	}

	/**
	 * Hits a given URL and returns the response string
	 * 
	 * @param url
	 *            String
	 * @return String
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String hitURL(String url) throws ClientProtocolException,
			IOException {

		HttpGet httpget = new HttpGet(url);
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = httpclient.execute(httpget, responseHandler);
		return responseBody;
	}

	public static int testServer() {

		try {
			HttpUtils.initialize();
			HttpGet httpget = new HttpGet("http://www.imdbapi.com/?t=titanic");
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			String responseBody;
			responseBody = httpclient.execute(httpget, responseHandler);
			HttpUtils.shutdown();
			return 0;
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			StatusScreen.printStatus("Error in Internet Connection Detected");
			return 1;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 1;
		}

	}

	// /////////////////////////////////////////////////////////

	public static int advertise() {

		try {
			HttpUtils.initialize();
			HttpGet httpget = new HttpGet(
					"http://www.electromedica.in/advertise.txt");
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			String responseBody;

			responseBody = httpclient.execute(httpget, responseHandler);
			StatusScreen.printStatus("\n\n" + responseBody + "\n\n");
			HttpUtils.shutdown();
			return 0;
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			StatusScreen.printStatus("Error in Internet Connection Detected");
			return 1;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 1;
		}

	}

	// ///////////////////////////////////////////////////////////////////////////
	/**
	 * Shuts down the client connection manager. Client should be reinitialized
	 * to be usable again.
	 */
	public static void shutdown() {
		httpclient.getConnectionManager().shutdown();
	}

}
